package com.lz.model;

import com.lz.constant.Constants;
import lombok.Data;

import java.io.Serializable;

/**
 * @Package com.lz.model
 * @Project currency-trading
 * @Author lenovo
 * @Date 2022/1/10 0:15
 * @Describe 公共的返回值对象
 * @Version V1.0
 */
@Data
public class ResultBean<T> implements Serializable {

    private static final long serialVersionUID = 2344273863226804571L;

    /**
     * 成功
     */
    public static final int SUCCESS = Constants.SUCCESS;

    /**
     * 失败
     */
    public static final int FAIL = Constants.FAIL;

    /**
     * 执行状态
     */
    private int code;

    /**
     * 成果信息or失败原因
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    /**
     * 成功只响应200，不带数据
     */
    public static <T> ResultBean<T> success() {
        return restResult(null, SUCCESS, null);
    }

    /**
     * 成功响应200，带数据
     */
    public static <T> ResultBean<T> success(T data) {
        return restResult(data, SUCCESS, null);
    }

    /**
     * 成功响应200，带数据和成功信息
     */
    public static <T> ResultBean<T> success(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    /**
     * 失败响应500，不带数据
     */
    public static <T> ResultBean<T> fail() {
        return restResult(null, FAIL, null);
    }

    /**
     * 失败响应500，带失败原因
     */
    public static <T> ResultBean<T> fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    /**
     * 失败响应500，带数据
     */
    public static <T> ResultBean<T> fail(T data) {
        return restResult(data, FAIL, null);
    }

    /**
     * 失败响应500，带数据和失败原因
     */
    public static <T> ResultBean<T> fail(T data, String msg) {
        return restResult(data, FAIL, msg);
    }

    /**
     * 失败带代码和信失败原因
     */
    public static <T> ResultBean<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    private static <T> ResultBean<T> restResult(T data, int code, String msg) {
        ResultBean<T> apiResult = new ResultBean<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
